package com.kma.summer2012.dao;

import com.kma.summer2012.factory.DAOFactory;
import com.kma.summer2012.factory.HibernateUtil;
import com.kma.summer2012.factory.SpringBeansFactory;
import com.kma.summer2012.model.CrumbsDateAndTime;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Администратор
 * Date: 15.01.13
 * Time: 18:49
 * To change this template use File | Settings | File Templates.
 */
public class CrumbsDateAndTimeDAOImpl implements CrumbsDateAndTimeDAO {
    @Override
    public void add(CrumbsDateAndTime dateAndTime) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(dateAndTime);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public CrumbsDateAndTime addDate(CrumbsDateAndTime dateAndTime) throws SQLException {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(dateAndTime);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return dateAndTime;
    }

    @Override
    public void update(CrumbsDateAndTime dateAndTime) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(dateAndTime);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public CrumbsDateAndTime getById(int dateAndTime_id) {
        Session session = null;
        CrumbsDateAndTime dateAndTime = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            dateAndTime = (CrumbsDateAndTime) session.get(CrumbsDateAndTime.class, dateAndTime_id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return dateAndTime;
    }

    @Override
    public Collection<CrumbsDateAndTime> getAll() {
        Session session = null;
        List dateAndTimes = new ArrayList<CrumbsDateAndTime>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            dateAndTimes = session.createCriteria(CrumbsDateAndTime.class).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return dateAndTimes;
    }

    @Override
    public void delete(CrumbsDateAndTime dateAndTime) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(dateAndTime);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public List<CrumbsDateAndTime> getByRequestId(int request_id) {
        Session session = null;
        List<CrumbsDateAndTime> dateAndTime = new ArrayList<CrumbsDateAndTime>();
        DAOFactory factory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            dateAndTime = session.createCriteria(CrumbsDateAndTime.class).add(Restrictions.eq("crumbsRequest", factory.getCrumbsRequestDAO().getById(request_id))).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return dateAndTime;
    }

}
